﻿<!doctype html>
<%@LANGUAGE="JScript" %>

<!--#include file ="Includes/JSON2.asp"-->
<!--#include file ="Includes/DB.asp"--> 
<!--#include file ="Includes/ASPHelpers.asp"-->

<%/*--- BEGIN ASP ------------------------------------------------------------------------------------*/

//-----checking to see what the COD settings are-------- 
var h = Helpers;
var fs = Server.CreateObject("scripting.filesystemobject");
var filepath = Server.MapPath("access/backend/CODs.txt");
var CODs = "";
var FromDispatch = "" + Request.QueryString("FromDispatch");
var ContractID = "" + h.Req("ContractID", "");
	  
if (!fs.FileExists(filepath)){
	fs.CreateTextFile(filepath);
}
				
var f = fs.OpenTextFile(filepath);
			
if (!f.AtEndOfStream){
	CODs = f.ReadLine();
}
else{
	CODs = "no";
}

f.Close();
//----end COD -----------------

var db, rsContract, rsAppliances, queryName, IsHS, 
	Title = "", 
	AccountNumber = "", 
	FirstName = "",
	LastName = "",
	Address = "",
	City = "",
	State = "",
	Zip = "",
	Phone = "",
	applianceHTML =	[],
	appliancePipeList = [];


if (!h.IsNullOrEmpty(ContractID)){
	/* if contractID is numeric then pull from customers table in homesmart_customers.mdb else pull from non_homesmart_customers in tickets db.
	 * iterate appliances and build html string of appliance names. also add pipe-delimited appliances to mbAppStr because that is used in a JS function */
	 
	
	//----get customer info---------------------	

	
	if(ContractID.length > 3 && ContractID.substring(0,3).toLowerCase() != "nhs"){
		IsHS = true;
		queryName = "getHSCust_By_ID";
	}
	else{
		IsHS = false;
		queryName = "getNonHSCust_By_ID";
	}
	
	db = new DB.DataCall();
	db.QueryName = 			queryName;
	db.ConnectionString = 	DB.ConnectionStrings.Hub;
	db.CommandType = 		DB.CommandTypes.StoredProc;
	
	db.AddParameter("@ContractID", DB.DataTypes.String, ContractID);
	
	db.Execute();
		
	if(db.Success){
		rsContract = db.RecordSet;
		
		if(rsContract.RecordCount > 0){
			rsContract.MoveFirst();
			
			FirstName = h.Trim(rsContract.fields("First_Name").value);
			LastName =	h.Trim(rsContract.fields("Last_Name").value);
			Address = 	h.Trim(rsContract.fields("Address").value);
			City =		h.Trim(rsContract.fields("City").value);
			State =		h.Trim(rsContract.fields("State").value);
			Zip =		h.Trim("" + rsContract.fields("Zip").value);
			Phone =		h.Trim(rsContract.fields("Phone").value);
			
		}
		
	 }
	 //----END get customer info ------------------------
	 
	 
	 //----get customer appliances---------------------
	 if(IsHS){
		db = new DB.DataCall();
		db.QueryName = 			"getHSCustAppliances";
		db.ConnectionString = 	DB.ConnectionStrings.Hub;
		db.CommandType = 		DB.CommandTypes.StoredProc;
		
		db.AddParameter("@ContractID", DB.DataTypes.String, ContractID);
		
		db.Execute();
			
		if(db.Success){
			rsAppliances = db.RecordSet;
			
			if(rsAppliances.RecordCount > 0){
				rsAppliances.MoveFirst();
				
				var appNum = 0;
				while(!rsAppliances.EOF){
					var curAppName = h.Trim(rsAppliances.fields("Appliance_Name").value);
					applianceHTML.push(	++appNum + ") &nbsp; &nbsp; " + curAppName + "<br>"	);
					appliancePipeList.push(curAppName);
					
					rsAppliances.MoveNext();
				}
			}
			else {	//recordcount = 0
				applianceHTML.push("(ERROR retrieving this HomeSmart customers appliances:  Contract_ID not matched.)");
			}
		 }		
	 }
	 else{	//if !IsHS
		applianceHTML.push("Non-HomeSmart Customer");
	 }
	 
	 //----end get customer appliances----------------
	 
	 

}


/*--- /END ASP -----------------------------------------------------------------------------------------*/%>


<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8" />

<TITLE><%= FirstName + " " + LastName %></TITLE>
<LINK HREF="UniversalStyleSheet.css" REL="stylesheet" TYPE="text/css" />
<STYLE TYPE="text/css">
body {
	margin-left: 1px;
	margin-top: 1px;
	margin-right: 1px;
	margin-bottom: 1px;
}
</STYLE>

<SCRIPT type="text/javascript">

function fnMadeChanges(what)
{
	var mc=document.getElementById("MadeChanges");
	mcv=mc.value;
	mcv=mcv + what + "\n";
	mc.value=mcv;
	document.getElementById("savemsg").innertext="Changes Need Saved";
}



function BtnSaveClick()
{
    var fn=document.getElementById("FirstName");
    var ln=document.getElementById("LastName");
    var ad=document.getElementById("Address");
    var c=document.getElementById("City");
    var s=document.getElementById("State");
    var z=document.getElementById("Zip");

    Fname=fn.value;
    Lname=ln.value;
    Addr=ad.value;
    Cty=c.value;
    St=s.value;
    Zipcode=z.value;
    msg="";

    if (Fname==""){msg=msg + "- First Name\n";} 

    if (Lname==""){msg=msg + "- Last Name\n";}

    if (Addr==""){msg=msg + "- Address\n";}

    if (Cty==""){msg=msg + "- City\n";}

    if (St==""){msg=msg + "- State\n";}

    if (Zipcode==""){msg=msg + "- Zip Code\n";}


    if (msg != ""){
	    alert( "Cant Save Changes. The Following Required Fields Were Blank:\n\n" + msg);
	    return;
        }

    <% 
		if( FromDispatch == "yes"){ %>
			document.FrmCustInfo.action="SaveAccount.asp?FromDispatch=yes";
    <%  } %>
		
    document.FrmCustInfo.onsubmit ="";
    document.FrmCustInfo.submit();
}
</SCRIPT>
</HEAD>

<BODY>

<TABLE WIDTH="500" HEIGHT="100" BORDER="0" ALIGN="left" CELLPADDING="0" CELLSPACING="0">
   <TR>
    <TD WIDTH="325" ALIGN="center" VALIGN="top">
	<FORM ACTION="SaveAccount.asp" METHOD="post" NAME="FrmCustInfo" ID="FrmCustInfo" onSubmit="javascript: return false;">
	<TABLE WIDTH="440" HEIGHT="200" BORDER="0" ALIGN="left" CELLPADDING="0" CELLSPACING="0">
	 <TR>
        <TD WIDTH="20" HEIGHT="20" BACKGROUND="Images/TableUpperLeft.png"></TD>
        <TD WIDTH="400" HEIGHT="20" BACKGROUND="Images/TableUpperMiddle.png"></TD>
        <TD WIDTH="20" HEIGHT="20" BACKGROUND="Images/TableUpperRight.png"></TD>
      </TR>
      <TR>
        <TD WIDTH="20" HEIGHT="159" BACKGROUND="Images/TableMiddleLeft.png"></TD>
        <TD WIDTH="400" BGCOLOR="#FFFFFF">
		
          <TABLE WIDTH="400" HEIGHT="145" BORDER="0" CELLPADDING="0" CELLSPACING="1" BORDERCOLOR="#FFFFFF" ID="CustInfo">
            <TR>
              <TD HEIGHT="22" ALIGN="right" VALIGN="top" BGCOLOR="#D2FFD2" CLASS="SearchFormText">
				<SPAN CLASS="formLabel">
					Account <BR>
					Number: 
				</SPAN>
			  </TD>
              <TD VALIGN="top" BGCOLOR="#D2FFD2" CLASS="SearchFormText">
				<INPUT NAME="AccountNumber" TYPE="text" ID="AccountNumber" SIZE="15" disabled="disabled" value="<%= ContractID %>" /></TD>
              <TD ALIGN="right" VALIGN="top" BGCOLOR="#D2FFD2" CLASS="SearchFormText">
				<SPAN CLASS="formLabel">
					Contract ID:
				</SPAN>
			  </TD>
              <TD VALIGN="top" BGCOLOR="#D2FFD2" CLASS="SearchFormText">
				<INPUT NAME="Contract_ID_Display" TYPE="text" ID="Contract_ID_Display" SIZE="15" disabled="disabled" value="<%= ContractID %>"/>
                <INPUT NAME="Contract_ID" TYPE="hidden" ID="Contract_ID" value="<%= ContractID %>" />
              </TD>
            </TR>
            <TR>
              <TD WIDTH="91" HEIGHT="22" ALIGN="right" VALIGN="top" BGCOLOR="#D2FFD2" CLASS="SearchFormText">
				<SPAN CLASS="formLabel">
					First<BR>
					Name:
				</SPAN>
              </TD>
              <TD WIDTH="114" HEIGHT="22" VALIGN="top" BGCOLOR="#D2FFD2" CLASS="SearchFormText">
				<INPUT NAME="FirstName" TYPE="text" ID="FirstName" TABINDEX="1" onChange="fnMadeChanges('First Name')" SIZE="15" MAXLENGTH="30" value="<%= FirstName %>"/>
              </TD>
              <TD WIDTH="78" ALIGN="right" VALIGN="top" BGCOLOR="#D2FFD2" CLASS="SearchFormText">
				<SPAN CLASS="formLabel">
					Last<BR>
					Name:
				</SPAN>
			</TD>
              <TD WIDTH="127" VALIGN="top" BGCOLOR="#D2FFD2" CLASS="SearchFormText">
				<INPUT NAME="LastName" TYPE="text" ID="LastName" TABINDEX="2" onChange="fnMadeChanges('Last Name')" SIZE="15" MAXLENGTH="50" value="<%= LastName %>"/>
              </TD>
            </TR>
            <TR>
				<TD HEIGHT="22" ALIGN="right" VALIGN="top" BGCOLOR="#D2FFD2" CLASS="SearchFormText">
					<SPAN CLASS="formLabel">
						Address:
					</SPAN>
				</TD>
				<TD HEIGHT="22" COLSPAN="3" VALIGN="top" BGCOLOR="#D2FFD2">
					<INPUT NAME="Address" TYPE="text" ID="Address" TABINDEX="3" onChange="fnMadeChanges('Address')" SIZE="40" MAXLENGTH="100" value="<%= Address %>"/>
					<INPUT NAME="MadeChanges" TYPE="hidden" ID="MadeChanges">
				</TD>
           </TR>
            <TR>
              <TD HEIGHT="22" ALIGN="right" VALIGN="top" BGCOLOR="#D2FFD2"></TD>
              <TD HEIGHT="22" COLSPAN="3" VALIGN="top" BGCOLOR="#D2FFD2">
				<INPUT NAME="City" TYPE="text" ID="City" TABINDEX="4" onChange="fnMadeChanges('City')" SIZE="20" MAXLENGTH="50" value="<%= City %>"/>
                <INPUT NAME="State" TYPE="text" ID="State" TABINDEX="5" onChange="fnMadeChanges('State')" SIZE="5" MAXLENGTH="2" value="<%= State %>"/>
                <INPUT NAME="Zip" TYPE="text" ID="Zip" TABINDEX="6" onChange="fnMadeChanges('Zip Code')" SIZE="5" MAXLENGTH="10" value="<%= Zip %>"/>
              </TD>
           </TR>
            <TR>
              <TD HEIGHT="22" ALIGN="right" VALIGN="top" BGCOLOR="#D2FFD2" CLASS="SearchFormText">
				<SPAN CLASS="formLabel">
					Phone:
				</SPAN>
              </TD>
              <TD HEIGHT="22" COLSPAN="3" VALIGN="top" BGCOLOR="#D2FFD2" CLASS="SearchFormText">
				<INPUT NAME="Phone" TYPE="text" ID="Phone" TABINDEX="7" onChange="fnMadeChanges('Phone Number')" MAXLENGTH="25" value="<%= Phone %>"/>
              </TD>
           </TR>
            <TR>
              <TD HEIGHT="33" COLSPAN="4" ALIGN="right" VALIGN="top" BGCOLOR="#FFFFFF" class="Buttons">
				  <SPAN ID="savemsg" STYLE="font-style:italic; color:#990000; display:block; text-align:right" CLASS="formLabel"></SPAN>
					<% if(FromDispatch != "yes" && CODs == "yes"){ %>
							<A HREF="customerinformation.asp">
								<IMG SRC="Images/NewCustomerButton.png" ALT="New Customer" WIDTH="60" HEIGHT="30" BORDER="0" />
							</A>
				  <% } %>
	                 
				  <A HREF="javascript:BtnSaveClick()">
					<IMG SRC="Images/SaveButton.png" ALT="Save" WIDTH="60" HEIGHT="30" BORDER="0" />
				  </A>
				 
					 
				 <% if (FromDispatch != "yes"){ %>          
						<A HREF="javascript:NewTicketClick()">
							<IMG SRC="Images/NewTicketButton.png" ALT="New Ticket" WIDTH="60" HEIGHT="30" BORDER="0" />
						</A>
				 <% } %>				 
			  </TD>
            </TR>
       </TABLE>        
	  </TD>
        <TD WIDTH="20" BACKGROUND="Images/TableMiddleRight.png"></TD>
      </TR>
      <TR>
        <TD WIDTH="20" HEIGHT="20" BACKGROUND="Images/TableLowerLeft.png"></TD>
        <TD WIDTH="400" HEIGHT="20" BACKGROUND="Images/TableLowerMiddle.png"></TD>
        <TD WIDTH="20" HEIGHT="20" BACKGROUND="Images/TableLowerRight.png"></TD>
      </TR>
    </TABLE>
	</FORM>	</TD>
    <TD WIDTH="225" ALIGN="center" VALIGN="top">
	<% 

		//customer appliances
		if (FromDispatch != "yes"){
			%>
			<SCRIPT type="text/javascript" LANGUAGE="javascript">
					parent.parent.HistoryFrame.location="TicketHistory.asp?Contract_ID=<%= ContractID %>"
			</SCRIPT>
		<% } %>

	<TABLE WIDTH="290" BORDER="0" ALIGN="left" CELLPADDING="0" CELLSPACING="0" id="ApplianceTable">
	  <TR> 
        <TD WIDTH="20" HEIGHT="20" BACKGROUND="Images/TableUpperLeft.png"></TD>
        <TD WIDTH="250" HEIGHT="20" BACKGROUND="Images/TableUpperMiddle.png"></TD>
        <TD WIDTH="20" HEIGHT="20" BACKGROUND="Images/TableUpperRight.png"></TD>
      </TR>
      <TR>
        <TD WIDTH="20" BACKGROUND="Images/TableMiddleLeft.png"></TD>
        <TD BGCOLOR="#FFFFFF" CLASS="Appliances">
		
			<% //middle cell of appliances table
				Response.Write(applianceHTML.join(""));
			%>
		</TD>
        <TD WIDTH="20" BACKGROUND="Images/TableMiddleRight.png"></TD>
      </TR>
      <TR>
        <TD WIDTH="20" HEIGHT="20" BACKGROUND="Images/TableLowerLeft.png"></TD>
        <TD WIDTH="250" HEIGHT="20" BACKGROUND="Images/TableLowerMiddle.png"></TD>
        <TD WIDTH="20" HEIGHT="20" BACKGROUND="Images/TableLowerRight.png"></TD>
      </TR>
    </TABLE></TD>
  </TR>
</TABLE>

<SCRIPT type="text/javascript">
function NewTicketClick()
    {

    var mc=document.getElementById("MadeChanges")
    var mcv=mc.value;
    var mbAppStr="<%= appliancePipeList.join("|") %>"; 
    var apparray=mbAppStr.split("|");
    var appstr="";

    for (x=0;x <= apparray.length; x++){
	    appstr += apparray[x] + "\n"
    }

    if (mcv != ""){
	    if (confirm ("You've made changes to the following element(s) of this account:" & vbcrlf & "_____________________ \n\n" + mcv + "_____________________\n\n" + "- Click 'OK' if you would like to save changes now and then click 'New Ticket' again after saving. \n\n- Click 'Cancel' if you would not like to save the changes you've made to this account and proceed with starting a new ticket.")){
		    BtnSaveClick();
		    return;
	    }
    }

    var CODs="<%= CODs %>";

    var cid=document.getElementById("Contract_ID");

    if (cid.value == "") {
	    if (CODs == "no") {
		    msg= "You cannot start a new ticket until you have a customer to associate the ticket with.\nSo if this is a HomeSmart customer then return to the 'Search' page to find and open this customers HomeSmart account.\n\nIf this is NOT a HomeSmart customer then you will need to explain to this customer that HomeSmart is not currently accepting calls for non-HomeSmart covered repairs.";
	        }
	    else{
		    msg= "You cannot start a new ticket until you have a customer to associate the ticket with.\nSo either pull up a customers account first or if you are entering new customer information\nthen you must save that information first so that the new customer has a Contract_ID \nfor the new ticket to be associated with.";
    	
	    }
	    alert (msg);
	    return;
    }

    if (isNaN(cid.value.substring(0,1))) {
	    if (CODs == "no") {
		    msg=" *THIS CUSTOMER IS A NON-HOMESMART CUSTOMER*\n\nCurrently, HomeSmart is not accepting repair calls for non-HomeSmart covered appliances outside of business hours.\nYou will need to inform this customer that HomeSmart is not able to accept this call at this time.";
		    alert (msg);
		    return;
		    }
	    else{
		    alert ("*THIS CUSTOMER IS A NON-HOMESMART CUSTOMER*\n\nRemember to remind them:\n - They will be paying a trip fee, possibly at 1.5 times the normal rate which will be quoted by the technician.\n - They will be responsible for paying for parts and labor quoted by the technician due at the time of service.");
	    }
	    var nonhs=1;
    }

    if (nonhs != 1){
	    if (confirm ("Is this ticket for one of this customers covered appliances?\n\n OK=Yes\nCancel=No") == false) {
		    if (CODs == "no") {
			    alert( "This is a non-HomeSmart covered appliance.\n\nCurrently, HomeSmart is only accepting calls for HomeSmart-covered appliances.\nYou will need to inform this customer that HomeSmart cannot accept a non-covered appliance repair call outside of standard business hours and that they will need to call HomeSmart during business hours the next business day because you are only taking calls for covered appliances.");
			    return;
			}
		    else{
			    alert( "*THIS IS A NON-HOMESMART COVERED REPAIR*\n\nRemember to remind them:\n - They will be paying a trip fee, possibly at 1.5 times the normal rate which will be quoted by the technician.\n - They will be responsible for paying for parts and labor quoted by the technician due at the time of service.");
    		
		    }
	    }
    }

    parent.TicketFrame.location="TicketWindow.asp?Contract_ID=" + cid.value;
	
    }
</SCRIPT>

</BODY>
</HTML>
